package com.danskin.wechat.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.util.StringUtil;
import com.danskin.wechat.entity.CarouselInfo;
import com.danskin.wechat.service.CarouselInfoService;
import com.danskin.wechat.util.FTPHelper;
import com.danskin.wechat.util.JsonUtils;
import com.danskin.wechat.util.Page;
/**
 * 轮播图
 * @author admin
 *
 */
@Controller
@RequestMapping("carouselInfo")
public class CarouselInfoController extends BaseController{
	@Resource
	private CarouselInfoService carouselInfoService;
	
	@RequestMapping(value = "queryCarouselInfoList", method = RequestMethod.GET)
	public String queryCarouselInfoList(CarouselInfo carouselInfo,Model model){
		return "carouselInfo/carouselInfo_list";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getCarouselInfoList", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object getCarouselInfoList(Page page,HttpServletRequest request) throws Exception {
		Map map = getRequestParameter(this.getRequest());
		map.put("mpId", this.getMpId());
		page.setMap(map);
		List<CarouselInfo> listCarouselInfo = carouselInfoService.getCarouselInfoList(page);
		return pagelistToJSONForBootStrapTable(listCarouselInfo, page);
	}
	/**
	 * 删除配置信息
	 */
	@RequestMapping(value = "deleteCarouselInfo", method = RequestMethod.POST)
	public  void  deleteCarouselInfo(HttpServletRequest request, HttpServletResponse response){
		boolean flag = false;
		String id = request.getParameter("id");
		if(StringUtil.isNotEmpty(id)){
			carouselInfoService.deleteCarouselInfo(id);
			flag = true;
		}
	    renderData(response, flag);
	}
	/**
	 * 新增配置
	 * @param carouselInfo
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "openCarouselInfo",method = RequestMethod.GET)
	public String openCarouselInfo(CarouselInfo carouselInfo,Model model){
		return "carouselInfo/addCarouselInfo";
	}
	/**
	 * 判断sort排序是否重复
	 * @param sku
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/toCheckSort", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String toCheckSort(String sort) throws Exception {
    	Map<String,Object> map=new HashMap<String, Object>();
    	List<CarouselInfo> cList = carouselInfoService.queryCarouselInfoBySort(sort,this.getMpId());
    	if(cList.size()>0){	
    		map.put("status", 400);
    		map.put("msg", "sort排序重复");
    	}else{
    		map.put("status", 200);
    		map.put("msg", "sort排序可用");
    	}
    	return JsonUtils.objectToJson(map);
    }
	/***
	 * 保存数据
	 * @param carouselInfo
	 * @param model
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "saveCarouselInfo",method = RequestMethod.POST)
	public String saveCarouselInfo(HttpServletRequest request,CarouselInfo carouselInfo,@RequestParam("picture")MultipartFile file) throws IllegalStateException, IOException{		
		String pictureUrl = null;
		if(!file.isEmpty()){
			//上传图片到ftp服务器并返回图片地址
			pictureUrl = FTPHelper.getInstance().uploadPictureAndReturnPictureUrl(file, "carousel",
					FTPHelper.uploadTemporaryFile());
		}
		carouselInfo.setImg(pictureUrl);
		carouselInfo.setMpId(this.getLoginUser().getMpId());
		carouselInfo.setType("1");
		carouselInfoService.saveCarouselInfoModel(carouselInfo);
		return "redirect:/carouselInfo/queryCarouselInfoList"; 
	}
	/***
	 * 修改数据
	 * @param request
	 * @param model
	 */
	@RequestMapping(value = "updateCarouselInfo",method = RequestMethod.GET)
	public  String  updateCarouselInfo(HttpServletRequest request,Model model){
		String id = request.getParameter("id");
		CarouselInfo carouselInfo = null;
		if(StringUtil.isNotEmpty(id)){
			carouselInfo = carouselInfoService.findCarouselInfobyId(id);
		}
		model.addAttribute("carouselInfo", carouselInfo);
		return "carouselInfo/updateCarouselInfo";
		
	}
	/***
	 * 保存修改记录
	 * @param carouselInfo
	 * @param model
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "saveUpdateCarouselInfo",method = RequestMethod.POST)
	public String  saveUpdateCarouselInfo(HttpServletRequest request,CarouselInfo carouselInfo,@RequestParam("picture")MultipartFile file) throws IllegalStateException, IOException{
		String pictureUrl = null;		
		if(!file.isEmpty()){
			//上传图片到ftp服务器并返回图片地址
			 pictureUrl = FTPHelper.getInstance().uploadPictureAndReturnPictureUrl(file, "carousel",
					 FTPHelper.uploadTemporaryFile());
		}
		carouselInfo.setMpId(this.getLoginUser().getMpId());
		carouselInfo.setImg(pictureUrl);
		carouselInfoService.updateCarouselInfo(carouselInfo);
		return "redirect:/carouselInfo/queryCarouselInfoList"; 
	}
	private void renderData(HttpServletResponse response, boolean data) {
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(data);
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}
	
}
